home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / MacGambit 2.0 / sources2 / Thomas / examples-from-book.text < prev    next >
Encoding:
Text File  |  1992-11-25  |  27.7 KB  |  1,089 lines  |  [TEXT/gamI]

  1. -*-Indented-Text-*-
  2.  
  3. This file contains the examples, taken directly from the Dylan manual.
  4. This file is not in an executable format.
  5.  
  6.  
  7. Page 27
  8.  
  9.    ? "abc"
  10.    "abc"
  11.    ? 123
  12.    123
  13.    ? foo:
  14.    foo:
  15.    ? #\a
  16.    #\a
  17.    ? #t
  18.    #t
  19.    ? #f
  20.    #f
  21.    ? (quote foo)
  22.    foo
  23.    ? 'foo
  24.    foo
  25.    ? '(1 2 3)
  26.    (1 2 3)
  27.  
  28.  
  29. Page 28-29
  30.  
  31.    ? <window>
  32.    {the class <window>}
  33.    ? concatenate
  34.    {the generic function concatenate}
  35.    ? (define my-variable 25)
  36.    my-variable
  37.    ? my-variable
  38.    25
  39.    ? (bind ((x 50))
  40.        (+ x x))
  41.    100
  42.    ? (setter element)
  43.    {the generic function (setter element)}
  44.    ? (define (setter my-variable) 20) 
  45.    (setter my-variable)
  46.    ? (setter my-variable)
  47.    20
  48.  
  49. Page 29
  50.  
  51.    ? (+ 3 4)
  52.    7
  53.    ? (* my-variable 3)
  54.    75
  55.    ? (* (+ 3 4) 5)
  56.    35
  57.    ? ((if #t + *) 4 5)
  58.    9
  59.  
  60. Page 30
  61.  
  62.    ; Creates and initializes a module variable
  63.    (define my-variable 25)
  64.    ; Sets the value to 12
  65.    (set! my-variable 12)
  66.    ; Returns 30. Uses lexical variables x and y.
  67.    (bind ((x 10) (y 20))
  68.       (+ x y))
  69.    ; Creates an anonymous method, which expects 2 
  70.    ; numeric arguments.
  71.    (method ((a <number>) (b <number>))
  72.       (list (- a b) (+ a b)))
  73.  
  74. Page 30
  75.  
  76.    ? (values 1 2 3)
  77.    1
  78.    2
  79.    3
  80.    ? (define-method edges ((center <number>)(radius <number>))
  81.        (values (- center radius) (+ center radius)))
  82.    edges
  83.    ? (edges 100 2)
  84.    98
  85.    102
  86.  
  87. Page 32
  88.  
  89.    ? foo
  90.    error: unbound variable foo
  91.    ? (define foo 10)
  92.    foo
  93.    ? foo
  94.    10
  95.    ? (+ foo 100)
  96.    110
  97.    ? bar
  98.    error: unbound variable bar
  99.    ? (define bar foo)
  100.    bar
  101.    ? bar
  102.    10
  103.    ? (define foo 20)
  104.    warning: redefining variable foo
  105.    ? foo
  106.    20
  107.    ? bar
  108.    10
  109.    ? (+ foo bar)
  110.    30
  111.  
  112. Page 33
  113.  
  114.    ? (bind ((number1 20))
  115.             (number2 30))
  116.        (+ number1 number2))
  117.    50
  118.  
  119. Page 33
  120.  
  121.    ? (bind ((x 20)
  122.             (y (+ x x)))
  123.        (+ y y))
  124.    80
  125.  
  126. Page 33
  127.  
  128.    ? (define foo 10)
  129.    foo
  130.    ? (+ foo foo)
  131.    20
  132.    ? (bind ((foo 35))
  133.        (+ foo foo))
  134.    70
  135.    ? (bind ((foo 20))
  136.        (bind ((foo 50))
  137.          (+ foo foo)))
  138.    100
  139.  
  140. Page 34
  141.  
  142.    ? (bind (((x <integer>) (sqrt 2)))
  143.            x)
  144.    error: 1.4142135623730951 is not an instance of <integer>
  145.  
  146.  
  147. Page 34
  148.  
  149.    ? (bind ((foo bar baz (values 1 2 3)))
  150.        (list foo bar baz))
  151.    (1 2 3)
  152.    ? (define-method opposite-edges ((center <number>)
  153.                                     (radius <number>))
  154.        (bind ((min max (edges center radius)))
  155.          (values max min)))
  156.    opposite-edges
  157.    ? (opposite-edges 100 2)
  158.    102
  159.    98
  160.  
  161. Page 34
  162.  
  163.    ? (bind ((x 10)
  164.             (y 20))
  165.        (bind ((x y (values y x)))
  166.          (list x y)))
  167.    (20 10)
  168.  
  169. Page 34
  170.  
  171.    ? (bind ((#rest nums (edges 100 2)))
  172.        nums)
  173.    (98 102)
  174.  
  175. Page 41
  176.  
  177.    ? (double 10)
  178.    error: unbound variable double.
  179.  
  180. Page 41
  181.  
  182.    ? (define-method double ((thing <number>))
  183.        (+ thing thing))
  184.    double
  185.    ? double
  186.    {the generic function double}
  187.    ? (double 10)
  188.    20
  189.  
  190. Page 41
  191.  
  192.    ? (double "the rain in Spain.")
  193.    error: no method for {the generic function double} was found
  194.           for the arguments ("the rain in Spain.")
  195.  
  196. Page 41
  197.  
  198.    ? (define-method double ((thing <sequence>))
  199.        (concatenate thing thing))
  200.    double
  201.    ? (double "the rain in Spain.")
  202.    "the rain in Spain.the rain in Spain."
  203.    ? (double '(a b c))
  204.    (a b c a b c)
  205.  
  206. Page 43
  207.  
  208.    ? (define-method show-rest (a #rest b)
  209.        (print a)
  210.        (print b)
  211.        #t)
  212.    show-rest
  213.    ? (show-rest 10 20 30 40)
  214.    10
  215.    (20 30 40)
  216.    #t
  217.    ? (show-rest 10)
  218.    10
  219.    ()
  220.    #t
  221.  
  222. Page 44
  223.  
  224.    (define-method percolate (#key (brand 'maxwell-house)
  225.                                   (cups 4)
  226.                                   (strength 'strong))
  227.      (make-coffee brand cups strength))
  228.    (define-method layout (widget #key (position: the-pos)
  229.                                       (size: the-size))
  230.      (bind ((the-sibling (sibling widget)))
  231.       (unless (= the-pos (position the-sibling))
  232.         (align-objects widget the-sibling the-pos the-size))
  233.  
  234. Page 44
  235.  
  236.    (percolate brand: 'folgers cups: 10)
  237.    (percolate strength: 'weak
  238.               brand: 'tasters-choice
  239.               cups: 1)
  240.    (layout my-widget position: (point 10 10)
  241.                      size: (point 30 50))
  242.    (layout my-widget size: (query-user-for-size))
  243.  
  244. Page 45
  245.  
  246.    ? (define-method show-keys (req1 req2 #key foo)
  247.        (format #t "requireds: ~a ~a~%" req1 req2)
  248.        (format #t "key: ~a" foo)
  249.        #t)
  250.    show-keys
  251.    ? (show-keys 'one 'two foo: 'three)
  252.    requireds: one two
  253.    key: three
  254.    #t
  255.    ? (show-keys foo: 'three)
  256.    requireds: foo: three
  257.    key: #f
  258.    #t
  259.  
  260. Page 46
  261.  
  262.    ? (define-method label ((x <object>) #key price)
  263.       (list price x))
  264.    label
  265.    ? (define-method label ((x <sequence>) #key unit-price)
  266.       (add x (* unit-price (length x))))
  267.    label
  268.    ? (define-method label ((x <list>) #rest info #key calories)
  269.       (add x calories))
  270.    label
  271.    ? (label 'grape price: 189 unit-price: 2)
  272.    error:  illegal keyword argument unit-price:.  Accepted keyword arguments are (price:).
  273.    ? (label 'grape price: 189)
  274.    (189 grape)
  275.    ? (label (vector 3 4 5) price: 189 unit-price: 2)
  276.    #(6 3 4 5)
  277.    ? (label (vector 3 4 5) protein: 7 fat: 8 calories: 9)
  278.    error:  illegal keyword argument protein:.  Accepted keyword arguments are (price: unit-price:).
  279.    ? (label (list 3 4 5) protein: 7 fat: 8 calories: 9)
  280.    (9 3 4 5)
  281.  
  282. Page 46
  283.  
  284.    ? (define-method test (the-req #rest the-rest
  285.                                   #key a b)
  286.        (print the-req)
  287.        (print the-rest)
  288.        (print a)
  289.        (print b))
  290.    test
  291.    ? (test 1 a: 2 b: 3 c: 4)
  292.    1
  293.    (a: 2 b: 3 c: 4)
  294.    2
  295.    3
  296.  
  297. Page 49
  298.  
  299.    (define-class <point> (<object>)
  300.      horizontal
  301.      vertical)
  302.  
  303. Page 49
  304.  
  305.    (horizontal my-point)
  306.  
  307. Page 49
  308.  
  309.    ((setter horizontal) my-point 10)
  310.  
  311. Page 50
  312.  
  313.    (set! (horizontal my-point) 10)
  314.  
  315. Page 51   
  316.    
  317.    ? (define-class <menu> (<object>)
  318.        title
  319.        action)
  320.  
  321. Page 55
  322.  
  323.    ? (define-class <rectangle> (<object>)
  324.         (top type: <integer>
  325.              init-value: 0
  326.              init-keyword: top:)
  327.         (left type: <integer>
  328.               init-value: 0
  329.               init-keyword: left:)
  330.         (bottom type: <integer>
  331.                 init-value: 100
  332.                 init-keyword: bottom:)
  333.         (right type: <integer>
  334.                init-value: 100
  335.                init-keyword: right:))
  336.    <rectangle>
  337.    ? <rectangle>
  338.    {the class <rectangle>}
  339.    ? (define my-rectangle (make <rectangle> top: 50 left: 50))
  340.    my-rectangle
  341.    ? (top my-rectangle)
  342.    50
  343.    ? (bottom my-rectangle)
  344.    100
  345.    ? (set! (bottom my-rectangle) 55)
  346.    55
  347.    ? (bottom my-rectangle)
  348.    55
  349.    ? (set! (bottom my-rectangle) 'foo)
  350.    error: foo is not an instance of <integer> while executing (setter bottom).
  351.  
  352.  
  353. Page 58
  354.    
  355.    (define-class <view> (<object>)
  356.      (position allocation: instance)
  357.      ...)
  358.    
  359.    (define-class <displaced-view> (<view>)
  360.      (position allocation: virtual)
  361.      ...)
  362.    
  363.    (define-method position ((v <displaced-view>))
  364.      (displace-transform (next-method v)))
  365.    
  366.    (define-method (setter position) ((v <displaced-view>)
  367.                                      new-position)
  368.      (next-method v (undisplace-transform new-position)))
  369.  
  370. Page 59
  371.  
  372.    (define-class <shape> (<view>)
  373.      (image allocation: virtual)
  374.      (cached-image allocation: instance init-value: #f)
  375.      ...)
  376.    
  377.    (define-method image ((shape <shape>))
  378.      (or (cached-image shape)
  379.          (set! (cached-image shape) (compute-image shape))))
  380.    
  381.    (define-method (setter image) ((shape <shape>) new-image)
  382.      (set! (cached-image shape) new-image))
  383.  
  384. Page 61
  385.  
  386.    ? (define foo 10)
  387.    10
  388.    ? foo             ;this is a variable
  389.    10                ;this is the variable's contents
  390.    ? (set! foo (+ 10 10))
  391.    20
  392.    ? foo
  393.    20
  394.    ? (setter element)                   ;this is a variable
  395.    {generic function (setter element)}  ;the variable's contents
  396.    ? (set! (setter element) %set-element)
  397.    {primitive function %set-element}
  398.    ? (id? (setter element) %set-element)
  399.    #t
  400.  
  401. Page 62
  402.  
  403.    ? (define foo (vector 'a 'b 'c 'd))
  404.    foo
  405.    ? foo
  406.    #(a b c d)
  407.    ? (element foo 2)
  408.    c
  409.    ? (set! (element foo 2) 'sea)
  410.    sea
  411.    ? (element foo 2)
  412.    sea
  413.    ? foo
  414.    #(a b sea d)
  415.  
  416. Page 64
  417.  
  418.    ? (define-method test ((thing <object>))
  419.        (if thing
  420.            #t
  421.            #f))
  422.    test
  423.    ? (test 'hello)
  424.    #t
  425.    ? (test #t)
  426.    #t
  427.    ? (test #f)
  428.    #f
  429.    
  430.    ? (define-method double-negative ((num <number>))
  431.         (if (< num 0)
  432.             (+ num num)
  433.             num))
  434.    double-negative
  435.    ? (double-negative 11)
  436.    11
  437.    ? (double-negative -11)
  438.    -22
  439.  
  440. Page 65
  441.  
  442.    ? (define-method show-and-tell ((thing <object>))
  443.         (if thing
  444.             (begin
  445.                (print thing)
  446.                #t)
  447.             #f))
  448.    show-and-tell
  449.    ? (show-and-tell "hello")
  450.    hello
  451.    #t
  452.  
  453. Page 65
  454.  
  455.    (when (bonus-illuminated? pinball post)
  456.        (add-bonus-score current-player 100000))
  457.  
  458. Page 65
  459.  
  460.    (unless (detect-gas? nose)
  461.        (light match))
  462.  
  463. Page 66
  464.    
  465.    (cond ((< new-position old-position)
  466.             "the new position is less")
  467.           ((= new-position old-position)
  468.            "the positions are equal")
  469.           (else: "the new position is greater"))
  470.  
  471. Page 67
  472.  
  473.    (case (career-choice student)
  474.       ((art music drama)
  475.        (print "Don't quit your day job."))
  476.       ((literature history linguistics)
  477.        (print "That really is fascinating."))
  478.       ((science math engineering)
  479.        (print "Say, can you fix my VCR?"))
  480.       (else: "I wish you luck."))
  481.  
  482. Page 67
  483.  
  484.    (select my-object instance?
  485.      ((<window> <view> <rectangle>) "it's a graphic object")
  486.      ((<number> <list> <sequence>) "it's something computational")
  487.      (else: "Don't know what it is"))
  488.  
  489. Page 68
  490.  
  491.    ? (if #t
  492.          (print "it was true")
  493.          #t
  494.          #f)
  495.    error:  too many arguments to if.
  496.    ? (if #t
  497.          (begin (print "it was true")
  498.                 #t)
  499.          #f)
  500.    "it was true"
  501.    #t
  502.  
  503. Page 69
  504.  
  505.    (define-method factorial ((n <integer>))
  506.       (for ((i n (- i 1))   ;variable clause 1
  507.             (v 1 (* v i)))  ;variable clause 2
  508.            ((<= i 0) v))    ;end test and result
  509.  
  510. Page 69
  511.  
  512.    (define-method first-even ((s <sequence>))
  513.      (for-each ((number s))
  514.                ((even? number) number)
  515.                                 ; No body forms needed
  516.         ))
  517.  
  518. Page 70
  519.  
  520.    (define-method schedule-olympic-games ((cities <sequence>)
  521.                                           (start-year <number>))
  522.       (for-each ((year (range from: start-year by: 4))
  523.                  (city cities))
  524.                 ()              ; No end test needed.
  525.          (schedule-game city year)))
  526.  
  527. Page 70
  528.  
  529.    ? (begin
  530.        (dotimes (i 6) (print "bang!"))
  531.        (print "click!"))
  532.    bang!
  533.    bang!
  534.    bang!
  535.    bang!
  536.    bang!
  537.    bang!
  538.    click!
  539.  
  540. Page 71
  541.  
  542.    ? (define-method first-even ((seq <sequence>))
  543.        (bind-exit (exit)
  544.          (do (method (item)
  545.                 (when (even? item)
  546.                   (exit item)))
  547.               seq)))
  548.    first-even
  549.    ? (first-even '(1 3 5 4 7 9 10))
  550.    4
  551.  
  552. Page 72
  553.  
  554.    ? +
  555.    {the generic function +}
  556.    ? '+
  557.    +
  558.    ? (quote +)
  559.    +
  560.    ? ''+
  561.    (quote +)
  562.    ? (+ 10 10)
  563.    20
  564.    ? '(+ 10 10)
  565.    (+ 10 10)
  566.    ? (quote (+ 10 10))
  567.    (+ 10 10)
  568.  
  569. Page 73
  570.  
  571.    ? (apply + 1 '(2 3))
  572.    6
  573.    ? (+ 1 2 3)
  574.    6
  575.    ? (define math-functions (list + * /  ))
  576.    math-functions
  577.    ? math-functions
  578.    ({method +} {method *} {method /} {method  })
  579.    ? (first math-functions)
  580.    {method +}
  581.    ? (apply (first math-functions) 1 2 '(3 4))
  582.    10
  583.  
  584. Page 79
  585.  
  586.    ? (method (num1 num2)
  587.        (+ num1 num2))
  588.    {an anonymous method}
  589.  
  590. Page 80
  591.  
  592.    ;the second argument to SORT is the test function
  593.    ? (sort person-list
  594.            (method (person1 person2)
  595.              (< (age person1)
  596.                 (age person2))))
  597.    ? (bind ((double (method (number)
  598.                       (+ number number))))
  599.        (double (double 10)))
  600.    40
  601.  
  602. Page 80
  603.  
  604.    ? (define-method double ((my-method <function>))
  605.        (method (#rest args)
  606.          (apply my-method args)
  607.          (apply my-method args)
  608.          #f))
  609.    double
  610.    ? (define print-twice (double print))
  611.    print-twice
  612.    ? print-twice
  613.    {an anonymous method}
  614.    ? (print-twice "The rain in Spain. . .")
  615.    The rain in Spain. . .The rain in Spain. . .
  616.    #f
  617.    ? (print-twice 55)
  618.    5555
  619.    #f
  620.  
  621. Page 81
  622.  
  623.    ? (define-method root-mean-square ((s <sequence>))
  624.         (bind-methods ((average (numbers)
  625.                          (/ (reduce1 + numbers)
  626.                             (length numbers)))
  627.                        (square (n) (* n n)))
  628.            (sqrt (average (map square s)))))
  629.    root-mean-square
  630.    ? (root-mean-square '(5 6 6 7 4))
  631.    5.692099788303083
  632.  
  633. Page 81
  634.  
  635.    ? (define-method newtons-sqrt (x)
  636.         (bind-methods ((sqrt1 (guess)
  637.                           (if (close? guess) 
  638.                         ringo)
  639.                    '(richard george edward charles john))
  640.    (john george)
  641.  
  642. Page 107
  643.  
  644.    ? (union '(butter flour sugar salt eggs)
  645.             '(eggs butter mushrooms onions salt))
  646.    (salt butter flour sugar eggs mushrooms onions)
  647.  
  648. Page 107
  649.  
  650.    ? (remove-duplicates '(spam eggs spam sausage spam spam spam))
  651.    (spam eggs sausage)
  652.  
  653. Page 108
  654.  
  655.    ? (remove-duplicates! '(spam eggs spam sausage spam spam))
  656.    (spam eggs sausage)
  657.    
  658. Page 108
  659.    
  660.    ? (define hamlet '(to be or not to be))
  661.    hamlet
  662.    ? (id? hamlet (copy-sequence hamlet))
  663.    #f
  664.    ? (copy-sequence hamlet start: 2 end: 4)
  665.    (or not)
  666.  
  667. Page 108
  668.  
  669.    ? (concatenate-as <string> '(#\n #\o #\n) '(#\f #\a #\t))
  670.    "nonfat"
  671.    ? (concatenate-as <vector> '(0 1 2) '(3 4 5) '(6 7 8))
  672.    #(0 1 2 3 4 5 6 7 8)
  673.  
  674. Page 108
  675.  
  676.    ? (concatenate "low-" "calorie")
  677.    "low-calorie"
  678.    ? (concatenate '(0 1 2) '(3 4 5) '(6 7 8))
  679.    (0 1 2 3 4 5 6 7 8)
  680.  
  681. Page 109
  682.  
  683.    ? (define phrase "I hate oatmeal.")
  684.    phrase
  685.    ? (replace-subsequence! phrase "like" start: 2)
  686.    "I like oatmeal."
  687.  
  688.  
  689. Page 109
  690.  
  691.    ? (define x '(bim bam boom))
  692.    x
  693.    ? (reverse x)
  694.    (boom bam bim)
  695.    ? x
  696.    (bim bam boom)
  697.  
  698.  
  699. Page 109
  700.  
  701.    ? (reverse! '(bim bam boom))
  702.    (boom bam bim)
  703.  
  704. Page 110
  705.  
  706.    ? (define numbers '(3 1 4 1 5 9))
  707.    numbers
  708.    ? (sort numbers)
  709.    (1 1 3 4 5 9)
  710.    ? numbers
  711.    (3 1 4 1 5 9)
  712.  
  713. Page 110
  714.  
  715.    ? (sort! '(3 1 4 1 5 9))
  716.    (1 1 3 4 5 9)
  717.  
  718. Page 110
  719.  
  720.    ? (last '(emperor of china))
  721.    china
  722.  
  723. Page 111
  724.  
  725.    ? (subsequence-position "Ralph Waldo Emerson" "Waldo")
  726.    6
  727.  
  728. Page 113
  729.  
  730.    ? (aref #(7 8 9) 1)
  731.    8
  732.  
  733. Page 113
  734.  
  735.    ? (set! (aref #(7 8 9) 1) 5)
  736.    #(7 5 9)                        ;buggy example.  Should return 5
  737.    ? ((setter aref) #(7 8 9) 1 5)
  738.    #(7 5 9)                        ;buggy example.  Should return 5
  739.  
  740. Page 113
  741.  
  742.    ? (dimensions (make <array> dimensions: '(4 4)))
  743.    (4 4)
  744.  
  745. Page 115
  746.  
  747.    ? (cons 1 2)
  748.    (1 . 2)
  749.    ? (cons 1 '(2 3 4 5))
  750.    (1 2 3 4 5)
  751.  
  752. Page 115
  753.  
  754.    ? (list 1 2 3)
  755.    (1 2 3)
  756.    ? (list (+ 4 3) (- 4 3))
  757.    (7 1)
  758.  
  759. Page 115
  760.  
  761.    ? (list* 1 2 3 '(4 5 6))
  762.    (1 2 3 4 5 6)
  763.  
  764.  
  765. Page 116
  766.  
  767.    ? (car '(4 5 6))
  768.    4
  769.    ? (car '())
  770.    ()
  771.  
  772. Page 116
  773.  
  774.    ? (cdr '(4 5 6))
  775.    (5 6)
  776.    ? (cdr '())
  777.    ()
  778.  
  779. Page 116
  780.  
  781.    ? (define x '(4 5 6))
  782.    (4 5 6)
  783.    ? (set! (car x) 9)
  784.    9
  785.  
  786. Page 116
  787.  
  788.    ? (define x '(4 5 6))
  789.    (4 5 6)
  790.    ? (set! (cdr x) '(a b c))
  791.    (a b c)
  792.  
  793. Page 120
  794.  
  795.    ? (define x "Van Gogh")
  796.    x
  797.    ? (as-lowercase x)
  798.    "van gogh"
  799.  
  800. Page 120
  801.  
  802.    ? (define x "Van Gogh")
  803.    x
  804.    ? (as-lowercase! x)
  805.    "van gogh"
  806.  
  807. Page 120
  808.  
  809.    ? (define x "Van Gogh")
  810.    x
  811.    ? (as-uppercase x)
  812.    "VAN GOGH"
  813.  
  814. Page 120
  815.  
  816.    ? (define x "Van Gogh")
  817.    x
  818.    ? (as-uppercase x)
  819.    "VAN GOGH"
  820.  
  821. Page 123
  822.  
  823.    (define-method do1 (f (c <collection>))
  824.      (for ((state (initial-state c) (next-state c state)))
  825.           ((not state) #f)
  826.        (f (current-element c state))))
  827.  
  828. Page 125
  829.  
  830.    (define-method key-sequence ((c <explicit-key-collection>))
  831.      (for ((state (initial-state c) (next-state c state))
  832.            (keys  '()               (cons (current-key c state)
  833.                                           keys)))
  834.           ((not state) keys)))
  835.  
  836. Page 125
  837.  
  838.    (define-method do-with-keys (f (c <explicit-key-collection>))
  839.      (for ((state (initial-state c) (next-state c state)))
  840.           ((not state) #f)
  841.        (f (current-key c state) (current-element c state))))
  842.  
  843. Page 126
  844.  
  845.    (define-method do-with-keys (f (c <sequence>))
  846.      (for ((state (initial-state c) (next-state c state))
  847.            (key   0                 (+ key 1)))
  848.           ((not state) #f)
  849.        (f key (current-element c state))))
  850.  
  851. Page 126
  852.  
  853.    (bind ((no-default (cons #f #f)))
  854.    
  855.     (define-method .i.element; ((c <explicit-key-collection>) key
  856.                             #key (default no-default))
  857.      (for ((state (initial-state c) (next-state c state)))
  858.           ((or (not state) (= (current-key c state) key))
  859.            (if state (current-element c state)
  860.                (if (id? default no-default)
  861.                    (error ...)
  862.                    default)))))
  863.     (define-method .i.element; ((c <sequence>) key
  864.                             #key (default no-default))
  865.       (for ((state (initial-state c) (next-state c state))
  866.             (k     0                 (+ k 1)))
  867.            ((or (not state) (= k key))
  868.             (if state (current-element c state)
  869.                 (if (id? default no-default)
  870.                     (error ...)
  871.                     default))))) )
  872.  
  873. Page 128
  874.  
  875.    (define-method (setter element) ((c <mutable-sequence>)
  876.                                     (key <integer>) new-value)
  877.      (for ((state (initial-state c) (next-state c state))
  878.            (k     0                 (+ k 1)))
  879.           ((or (not state) (= k key))
  880.            (if state
  881.                (set! (current-element c state) new-value)
  882.                (error ...)))))
  883.  
  884. Page 128
  885.  
  886.    (define-method (setter element) ((c <mutable-explicit-key-collection>)
  887.                                     key new-value)
  888.      (for ((state (initial-state c) (next-state c state)))
  889.           ((or (not state) (= (current-key c state) key))
  890.            (if state
  891.                (set! (current-element c state) new-value)
  892.                (error ...)))))
  893.  
  894. Page 129
  895.  
  896.    (define-method do2 (f (c1 <collection>) (c2 <collection>))
  897.      (bind ((keys (intersection (key-sequence c1)
  898.                                 (key-sequence c2))))
  899.        (for ((ks (initial-state keys) (next-state keys ks)))
  900.             ((not ks) #f)
  901.          (bind ((key (current-element keys ks)))
  902.            (f (element c1 key) (element c2 key))))))
  903.  
  904. Page 129
  905.  
  906.    (define-method do2 (f (c1 <sequence>) (c2 <sequence>))
  907.      (for ((s1 (initial-state c1) (next-state c1 s1))
  908.            (s2 (initial-state c2) (next-state c2 s2)))
  909.           ((or (not s1) (not s2)) #f)
  910.        (f (current-element c1 s1) (current-element c2 s2))))
  911.  
  912. Page 130
  913.  
  914.    (define-method map-into1 ((target <mutable-collection>) f
  915.                              (source <collection>))
  916.      (bind ((keys (intersection (key-sequence target)
  917.                                 (key-sequence source))))
  918.        (for ((ks (initial-state keys) (next-state keys ks)))
  919.             ((not ks) target)
  920.          (bind ((key (current-element keys ks)))
  921.            (set! (element target key) (f (element source key)))))))
  922.    (define-method map-into1 ((target <mutable-sequence>) f
  923.                              (source <sequence>))
  924.      (for ((ss (initial-state source) (next-state source ss))
  925.            (ts (initial-state target) (next-state target ts)))
  926.           ((or (not ss) (not ts)) target)
  927.        (set! (current-element target ts)
  928.              (f (current-element source ss)))))
  929.  
  930. Page 142
  931.  
  932.    (handler-case (some-function)
  933.      ((<type-error>) "there was a type-error")
  934.      ((<error>) "there was an error")
  935.      ((<warning>) "there was a warning"))
  936.  
  937. Page 144-146
  938.  
  939.    ;;; Classes such as <file-not-found> used in these examples are
  940.    ;;; invented for the example and are not part of the specification
  941.    ;;; This example shows minimal handling of a file-not-found error
  942.    
  943.    (handler-case (open "file-that-doesnt-exist")
  944.      ((<file-not-found> condition: c
  945.        (format *error-output* "~&The file ~A was not found."
  946.                (file-name c))))
  947.    
  948.    
  949.    ;;; This example shows how to handle a file-not-found error by
  950.    ;;; reading a different file instead.
  951.    (handler-bind (<file-not-found>
  952.                     (method (condition next-handler)
  953.                       (signal (make <try-a-different-file>
  954.                                     file-name: "my-emergency-backup-file"))))
  955.       (open "file-that-doesnt-exist")
  956.      ....)
  957.    
  958.    (define-method open (the-file)
  959.      (handler-case (guts-of-open the-file)
  960.        ((<try-a-different-file>
  961.          description: (method (stream)
  962.                         (format stream "Read a different file instead of ~A"
  963.                                         the-file))
  964.          condition: restart
  965.         (open (file-name restart)))))))
  966.    
  967.    (define-method guts-of-open (the-file)
  968.      (bind ((result (operating-system-open the-file)))
  969.        (cond ((instance? result <stream>) result)
  970.              ((id? result +file-not-found-error-code+)
  971.               (error (make <file-not-found> file-name: the-file)))
  972.              ...)))
  973.    
  974.    (define-class <file-not-found> (<error>)
  975.      ((file-name init-keyword: file-name:)))
  976.    
  977.    (define-method print ((self <file-not-found>) #key stream verbose)
  978.      (if verbose
  979.          (next-method)
  980.          (format stream "The file ~A was not found" (file-name self))))
  981.    
  982.    (define-class <try-a-different-file> (<restart>)
  983.      ((file-name init-keyword: file-name:)))
  984.    
  985.    
  986.    ;;; This is the same example improved so the restart handler that
  987.    ;;; reads another file can only be reached by a handler for the
  988.    ;;; associated condition, useful if there are nested errors.
  989.    
  990.    (handler-bind (<file-not-found>)
  991.                     (method (condition next-handler)
  992.                       (signal (make <try-a-different-file>
  993.                                     condition: condition
  994.                                     file-name: "my-emergency-backup-file")))
  995.      (open "file-that-doesnt-exist")
  996.      ....)
  997.    
  998.    (define-method open (the-file)
  999.      ....  (guts-of-open the-file))
  1000.    
  1001.    (define-method guts-of-open (the-file)
  1002.      (bind ((result (operating-system-open the-file)))
  1003.        (cond ((instance? result <stream>) result)
  1004.              ((id? result +file-not-found-error-code+)
  1005.               (bind ((condition (make <file-not-found> file-name: the-file)))
  1006.                 (handler-case (error condition)
  1007.                   ((<try-a-different-file>
  1008.                      test: (compose (curry id? condition) restart-condition)
  1009.                     description: (method (stream)
  1010.                                    (format stream
  1011.                                      "Read a different file instead of ~A"
  1012.                                      the-file))
  1013.                     condition: restart
  1014.                    (open (file-name restart)))))))
  1015.              ...)))
  1016.    
  1017.    (define-class <file-not-found> (<error>)
  1018.      ((file-name init-keyword: file-name:)))
  1019.    
  1020.    (define-method print ((self <file-not-found>) #key stream verbose)
  1021.      (if verbose
  1022.         (next-method)
  1023.         (format stream "The file ~A was not found" (file-name self))))
  1024.    
  1025.    (define-class <try-a-different-file> (<restart>)
  1026.      ((condition init-keyword: condition: reader: restart-condition)
  1027.       (file-name init-keyword: file-name:)))
  1028.  
  1029. Page 153
  1030.  
  1031.    ? (as <symbol> "foo")
  1032.    foo
  1033.    ? (id? 'FOO (as <symbol> "Foo"))
  1034.    #t
  1035.    ? 'Foo
  1036.    foo
  1037.    ? (as <keyword> "foo")
  1038.    foo:
  1039.  
  1040. Page 154
  1041.  
  1042.    ? (as <string> 'Foo)
  1043.    "foo"
  1044.    ? (as <string> 'bar:)
  1045.    "bar"
  1046.  
  1047. Page 157
  1048.  
  1049.    ? (define-method sum ((numbers <sequence>))
  1050.          (reduce1 + numbers))
  1051.    sum
  1052.    ? (define-method square ((x <number>)) (* x x))
  1053.    square
  1054.    ? (define-method square-all ((coords <sequence>))
  1055.        (map square coords))
  1056.    square-all
  1057.    ? (define distance (compose sqrt sum square-all))
  1058.    distance
  1059.    ? (distance '(3 4 5))
  1060.    7.0710678118654755
  1061.  
  1062. Page 157
  1063.  
  1064.    ? (map female? '(michelle arnold roseanne))
  1065.    (#t #f #t)
  1066.    ? (map (complement female?) '(michelle arnold roseanne))
  1067.    (#f #t #f)
  1068.  
  1069. Page 158
  1070.  
  1071.    ? (map (curry + 1) '(3 4 5))
  1072.    (4 5 6)
  1073.  
  1074. Page 158
  1075.  
  1076.    ? (define yuppify (rcurry concatenate ", ayup"))
  1077.    yuppify
  1078.    ? (yuppify "I'm from New Hampsha")
  1079.    "I'm from New Hampsha, ayup"
  1080.  
  1081. Page 159
  1082.  
  1083.    ? ((always 1) 'x 'y 'z)
  1084.    1
  1085.    ? ((always #t) #f #f)
  1086.    #t
  1087.  
  1088. $Id: examples-from-book.text,v 1.3 1992/09/25 13:47:57 birkholz Exp $
  1089.